package org.elfen.config;

import org.elfen.common.filter.BaseTokenFilterOption;
import org.elfen.common.filter.CsrfFilter;
import org.elfen.common.filter.TokenFilter;
import org.elfen.common.filter.XssFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 采用SpringBoot配置bean的方式进行配置的BeanFilterRegistrationBean
 * 
 */
@Configuration
public class MyFilterConfig {
	@Value("${token.white.urls}")
	private String tokenWhiteUrls;
	
	@Value("${csrf.white.urls}")
	private String csrfWhiteUrls;
	
	@Value("${xss.white.urls}")
	private String xssWhiteUrls;
	
	@Autowired
	private BaseTokenFilterOption baseTokenFilterAction;
	
	/**
	 * token的Filter
	 */
	@Bean
    public FilterRegistrationBean tokenFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new TokenFilter(tokenWhiteUrls,baseTokenFilterAction));
        registration.addUrlPatterns("/*");
        return registration;
    }
	
	/**
	 * Csrf的Filter
	 */
	@Bean
    public FilterRegistrationBean csrfFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new CsrfFilter(csrfWhiteUrls));
        registration.addUrlPatterns("/*");
        return registration;
    }
	
	/**
	 * XSS的Filter
	 */
	@Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new XssFilter(xssWhiteUrls));
        registration.addUrlPatterns("/*");
        return registration;
    }
}
